require "common"

--判断字符串
function judge_str( num , max)
        local out = ""
        local st = {}
        if num == nil then return nil end
        local len = string.len( num )
		if len % 2 ~= 0 then return 0 end

        for i=1,len,2 do
                c = string.sub(num,i,i+1)
                if tonumber(c) > max or tonumber(c) < 1 then return nil end

        end
        return true
end

--数字字符串排序

function sort_str_syw( num )
	num = string.gsub( num, "*", "")
	local out = ""
	local st = {}
	local len = string.len( num )
	for i=1,len,2 do
		table.insert( st, string.sub(num,i,i+1) )
	end
	table.sort(st)
	for i=1,len / 2 do
		out = out..st[i]
	end

	return out

end
--获取字符串中唯一的字符个数

function get_distinct_num( num )
	local dist = {}
	local str = {}
	local out = ""
	local len = string.len( num )
	str = sort_str_syw(num)
	for i=1,len ,2 do
		c = string.sub(str,i,i+1)
		if not in_table( c, dist ) then
			table.insert( dist, c )
		end
	end
	for i = 1,table.getn(dist) do
		out = out..dist[i]
	end
	return	out , table.getn(dist)

end


--返回注数,倍投数 , 投注串, 指令串
function random_syw( butType, inStr )
	local butNum = 1
	local butPower = 1
	local zlStr = ""
	local butStr = ""
	local itemList = {}
	local itemLen = 0;
	itemList = Split( inStr, "#B" )
	itemLen = table.getn( itemList )
	if itemList[1] == '' then return 0 end
	--任5
	if butType == 'f' then
		butNum = tonumber(itemList[1])
		if itemLen>2 or itemLen<1 or butNum ~= 1 then
			return 0
		end
		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >999 or butPower < 1 then return 0 end

		local nums = sel_dist_num(5,01,02,03,04,05,06,07,08,09,10,11)
		zlStr = zlStr..format_num(nums[1])..format_num(nums[2])..format_num(nums[3])..format_num(nums[4])..format_num(nums[5])
		zlStr=sort_str(zlStr, 11)
		zlStr = zlStr.."#B"..butPower
		butNum,butPower,butStr = check_syw('f', zlStr)

	--前三直选
	elseif butType == '3' then
		butNum = tonumber(itemList[1])
		if itemLen>2 or itemLen<1 or butNum ~= 1 then
			return 0
		end
		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >999 or butPower < 1 then return 0 end

		local nums = sel_dist_num(3,01,02,03,04,05,06,07,08,09,10,11)
		zlStr = zlStr..format_num(nums[1])..format_num(nums[2])..format_num(nums[3])
		zlStr = sort_str(zlStr, 11)
		zlStr = zlStr.."#B"..butPower
		butNum,butPower,butStr = check_syw('3', zlStr)
	--前三组选
	elseif butType == '8' then
		butNum = tonumber(itemList[1])
		if itemLen>2 or itemLen<1 or butNum ~= 1 then
			return 0
		end
		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >999 or butPower < 1 then return 0 end

		local nums = sel_dist_num(3,01,02,03,04,05,06,07,08,09,10,11)
		zlStr = zlStr..format_num(nums[1])..format_num(nums[2])..format_num(nums[3])
		zlStr = sort_str(zlStr, 11)
		zlStr = zlStr.."#B"..butPower
		butNum,butPower,butStr = check_syw('8', zlStr)
	----前二直选
	elseif butType == '2' then
		butNum = tonumber(itemList[1])
		if itemLen>2 or itemLen<1 or butNum ~= 1 then
			return 0
		end
		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >999 or butPower < 1 then return 0 end

		local nums = sel_dist_num(2,01,02,03,04,05,06,07,08,09,10,11)
		zlStr = zlStr..format_num(nums[1])..format_num(nums[2])
		zlStr = sort_str(zlStr, 11)
		zlStr = zlStr.."#B"..butPower
		butNum,butPower,butStr = check_syw('2', zlStr)
	----前二组选
	elseif butType == '7' then
		butNum = tonumber(itemList[1])
		if itemLen>2 or itemLen<1 or butNum ~= 1 then
			return 0
		end
		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >999 or butPower < 1 then return 0 end

		local nums = sel_dist_num(2,01,02,03,04,05,06,07,08,09,10,11)
		zlStr = zlStr..format_num(nums[1])..format_num(nums[2])
		zlStr = sort_str(zlStr, 11)
		zlStr = zlStr.."#B"..butPower
		butNum,butPower,butStr = check_syw('7', zlStr)
	else
		return 0
	end
	return butNum, butPower, butStr, zlStr

end


function check_syw( butType, inStr)
	local qxStr;
	local qjStr;
	local butNum = 0;
	local butPower = 1;
	local butStr = "";
	local itemList = {}
	local itemLen = 0;
	itemList = Split( inStr, "#B" )
	itemLen = table.getn( itemList )

	qjStr = string.gsub( inStr, "#B", "0")
	qxStr = string.gsub( qjStr, "#", "0")
	if itemLen > 2 or itemLen < 1 or string.find(qxStr,"%D") then return 0 end

	if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
	if butPower >999 or butPower < 1 then return 0 end
	xItem = Split(itemList[1], "#")

	--单式前一直选  1111#注码 1112#注码 1113#注码
	if butType == '1' or butType == '2' or butType == '3' then
		if #xItem > 5 or #xItem < 1 then return 0 end
		butNum = #xItem
		for i = 1, #xItem do
			_,distNum = get_distinct_num(xItem[i])
			if string.len(xItem[i]) / 2 ~= tonumber(butType) then return 0 end
			if distNum ~= tonumber(butType) then return 0 end
			if  not judge_str(xItem[i],11) then return 0 end
		end
	--复式自选
	elseif butType == '4' or butType == '5' or butType == '6' then
		butType = tonumber(butType) - 3
		if #xItem ~= butType then return 0 end
		if butType ==2 then
			if  is_str_mix(xItem[1],xItem[2])  then return 0 end
		elseif butType == 3 then
			if  is_str_mix(xItem[1],xItem[2]) or  is_str_mix(xItem[1],xItem[3]) or is_str_mix(xItem[2],xItem[3]) then return 0 end
		end

		butNum = 1
		for i = 1, #xItem do
			_,distNum = get_distinct_num(xItem[i])
			if distNum ~= string.len(xItem[i]) / 2 then return 0 end
			if  not judge_str(xItem[i],11) then return 0 end
			butNum = butNum*string.len(xItem[i])/2
		end
	--	前二组选 前三组选
	elseif butType == '7' or butType == '8' then
		butType =tonumber(butType) - 5

		if #xItem < 1 or #xItem > 5  then return 0 end
		if #xItem == 1 and string.len(xItem[1]) / 2 < butType then return 0 end

		if #xItem > 1 then
			for i=1 ,#xItem do
				len = string.len(xItem[i]) / 2
				_,distNum = get_distinct_num(xItem[i])
				if distNum ~= len  then return 0 end
				if len ~= butType then return 0 end
				if  not judge_str(xItem[i],11) then return 0 end
				butNum = #xItem
			end
		else
			len = string.len(xItem[1]) / 2
			_,distNum = get_distinct_num(xItem[1])
			if distNum ~= len  then return 0 end
			if len < butType or len > 11 then return 0 end
			if  not judge_str(xItem[1],11) then return 0 end
			butNum = combination(len, butType)
		end
	--前二组选胆拖
	elseif butType == '9' or butType == 'a'then
		if   not judge_str(xItem[1],11) or not judge_str(xItem[2],11) then return 0 end
		if #xItem ~= 2  or is_str_mix(xItem[1],xItem[2]) then return 0 end
		if butType == '9' then
			if string.len(xItem[1]) ~= 2 or string.len(xItem[2]) < 2 then return 0 end
			_,distNum = get_distinct_num(xItem[2])
			if distNum ~= string.len(xItem[2]) / 2 then return 0 end
			butNum = combination(string.len(xItem[2])/2,2 - string.len(xItem[1])/2)
		elseif butType == 'a' then
			if string.len(xItem[1]) < 2 or string.len(xItem[1]) > 4 or string.len(xItem[1]) + string.len(xItem[2]) < 6 then return 0 end
			_,distNumD = get_distinct_num(xItem[1])
			_,distNumT = get_distinct_num(xItem[2])
			if distNumD ~= string.len(xItem[1]) / 2 or distNumT ~= string.len(xItem[2]) / 2 then return 0 end
			butNum = combination(string.len(xItem[2])/2,3 - string.len(xItem[1])/2)
		end
	--任选
	elseif butType == 'b' or butType == 'c' or butType == 'd' or butType == 'e' or butType == 'f' or butType == 'g' or butType == 'h'  or butType == 'i' then
		butType = string.byte(butType) - string.byte('a')
		if #xItem < 1 or #xItem > 5  then return 0 end
		if #xItem == 1 and string.len(xItem[1]) / 2 < butType then return 0 end
		if butType == 1 and #xItem ~=1 then return 0 end
		if #xItem > 1 then
			for i = 1, #xItem do
				if  string.len(xItem[i]) / 2 ~= butType then return 0 end
				_,distNum = get_distinct_num(xItem[i])
				if distNum ~= string.len(xItem[i]) / 2 then return 0 end
				if  not judge_str(xItem[i],11) then return 0 end
				if butType == 8 and string.len(xItem[1]) / 2 ~= 8 then return 0 end
			end
			butNum = #xItem
		else
			if butType == 8 and string.len(xItem[1]) / 2 < 8 then return 0 end
			_,distNum = get_distinct_num(xItem[1])
			if distNum ~= string.len(xItem[1]) / 2 then return 0 end
			if  not judge_str(xItem[1],11) then return 0 end
			butNum = combination(string.len(xItem[1])/2,butType)
		end


	--任选胆拖
	elseif butType == 'j' or butType == 'k' or butType == 'l' or butType == 'm' or butType == 'n' or butType == 'o' or  butType == 'p' then
		butType = string.byte(butType) - string.byte('i') + 1
		if string.len(xItem[1]) < 2 or string.len(xItem[1])/2 > butType - 1  or string.len(xItem[1]) + string.len(xItem[2]) < butType *2 or is_str_mix(xItem[1],xItem[2]) then return 0  end

		if #xItem ~= 2 then return 0 end
		_,distNumD = get_distinct_num(xItem[1])
		_,distNumT = get_distinct_num(xItem[2])
		if distNumD ~= string.len(xItem[1]) / 2 or distNumT ~= string.len(xItem[2]) / 2 then return 0 end
		if   not judge_str(xItem[1],11) or not judge_str(xItem[2],11) then return 0 end
		butNum = combination(string.len(xItem[2])/2,butType - string.len(xItem[1])/2)
	else
		return 0
	end

	return butNum,butPower,inStr,inStr
end



--=================11选5兑奖============================
function query_syw(butType,queryStr,djStr)
	local nBingo = {0,0,0,0,0,0,0,0,0,0,0,0,0}
	local temp 	 = {0,0,0,0,0,0,0,0,0,0,0,0,0}
	local butPower = 1
	local bingNum = 0
	local bingoD = 0
	local bingoT = 0
	local bullStr = {}
	local szTemp = {}

	itemList = Split( queryStr, "#B" )
	if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end

	for i = 1, string.len(djStr), 2 do
		bullStr[(i+1)/2] = string.sub(djStr,i,i + 1)
	end

	if butType == '1' or butType == '2' or butType == '3' then
		jItem = Split(itemList[1], "#")
		djStr = string.sub(djStr, 1, tonumber(butType)*2)
		for i = 1, #jItem do
			if string.find(djStr, jItem[i]) ~= nil then
				nBingo[8 + tonumber(butType)] = nBingo[8 + tonumber(butType)] + 1
			end
		end
	elseif butType == '4' or butType == '5' or butType == '6' then
		jItem = Split(itemList[1], "#")
		nNum  = table.getn(jItem)
		for i = 1, nNum do
			szTemp[i] = jItem[i]
		end
		for i = 1, nNum do
			for j=1, string.len(szTemp[i]), 2 do
				if string.sub(szTemp[i], j, j+1 ) == bullStr[i] then
					bingNum = bingNum + 1
				end
			end
		end
		if bingNum == tonumber(butType) - 3 then
			nBingo[8 + tonumber(butType) - 3] = nBingo[8 + tonumber(butType) -3] + 1
		end
	elseif butType == '7'  then
		djStr = string.sub(djStr, 1, 4)
		jItem = Split(itemList[1], "#")
		for i=1, #jItem do
			bingNum = ConputeBingoNum(jItem[i] ,djStr ,2)
			if bingNum == 2 then
				nBingo[12] = nBingo[12] + 1
			end
		end
	elseif butType == '8'  then
		djStr = string.sub(djStr, 1, 6)
		jItem = Split(itemList[1], "#")

		for i=1, #jItem do
			bingNum = ConputeBingoNum(jItem[i] ,djStr ,2)
			if bingNum == 3 then
				nBingo[13] = nBingo[13] + 1
			end
		end
	elseif butType == '9' then
		jItem = Split(itemList[1], "#")
		djStr = string.sub(djStr, 1, 4)
		bingoD =ConputeBingoNum(jItem[1],djStr, 2)
		bingoT = ConputeBingoNum(jItem[2],djStr, 2)
		if bingoD == string.len(jItem[1]) / 2 then
			if bingoT == 1 then
				nBingo[12] = nBingo[12] + 1
			end
		end
	elseif butType == 'a' then
		jItem = Split(itemList[1], "#")
		djStr = string.sub(djStr, 1, 6)
		bingoD =ConputeBingoNum(jItem[1],djStr, 2)
		bingoT = ConputeBingoNum(jItem[2],djStr, 2)
		if bingoD == string.len(jItem[1]) / 2 then
			if bingoT == 3 - bingoD then
				nBingo[13] = nBingo[13] + 1
			end
		end
	elseif butType == 'b'  then
		djStr = string.sub(djStr,1,2)
		jItem = Split(itemList[1], "#")
		for i = 1, #jItem do
			bingo = ConputeBingoNum(jItem[i],djStr, 2)
			if  bingo == 1 then
				nBingo[1] = nBingo[1] + 1
			end
		end


	elseif  butType == 'c' or butType == 'd' or butType == 'e' or butType == 'f' or butType == 'g' or butType == 'h'  or butType == 'i' then
		butType = string.byte(butType) - string.byte('a')

		jItem = Split(itemList[1], "#")
		for i = 1, #jItem do
			bingoNum = ConputeBingoNum(jItem[i], djStr, 2)
			if butType <= 5 then
				nBingo[butType] = nBingo[butType] +  combination(bingoNum, butType)
			elseif butType > 5 then
				if bingoNum == 5 then
					nBingo[butType] = nBingo[butType] + combination(string.len(jItem[i])/2 - 5, butType - 5)
				end
			end
		end
	elseif butType == 'j' or butType == 'k' or butType == 'l' or butType == 'm' or butType == 'n' or butType == 'o' or  butType == 'p' then
		jItem = Split(itemList[1], "#")
		bingoD =ConputeBingoNum(jItem[1],djStr, 2)
		bingoT = ConputeBingoNum(jItem[2],djStr, 2)
		butType = string.byte(butType) - string.byte('i') + 1
		if bingoD == string.len(jItem[1]) / 2 then
			if butType <= 5 then
				nBingo[butType] = combination(bingoT, butType - bingoD)
			elseif butType > 5 then
				if (bingoD + bingoT) == 5 then
					nBingo[butType] = combination(string.len(jItem[2])/2 - bingoT, butType - 5)

				end
			end
		elseif bingoD ~= string.len(jItem[1]) / 2 then
			lenD = string.len(jItem[1]) / 2
			lenT = string.len(jItem[2]) / 2
			if lenD + bingoT <= butType and bingoD + bingoT == 5 then
				nBingo[butType] = combination(lenT- bingoT, butType - bingoT - lenD )
			end


		end
	end
	for i=1 ,13 do
		nBingo[i] = nBingo[i] * butPower
	end
	return nBingo[1]..","..nBingo[2]..","..nBingo[3]..","..nBingo[4]..","..nBingo[5]..","..nBingo[6]..","..nBingo[7]..","..nBingo[8]..","..nBingo[9]..","..nBingo[10]..","..nBingo[11]..","..nBingo[12]..","..nBingo[13]
end


--print(random_syw('f',"1#B10"))
--print(check_syw('i',"01020304050607080911#B10"))
--print(check_syw('6',"010203#11#0506#B10"))
--print(check_syw('7',"010203#B10"))
--print(check_syw('d',"010203#010204#B1"))
--print(check_syw('i',"010203040506070809#B10"))
--print(check_syw('j',"0102#03040506070809#B10"))
--print(query_syw('7',"010203#B1","0102030405"))
--print(query_syw('g',"010203040506070809","0102030405"))
--print(query_syw('p',"01#0203040506070809","0102030405"))
--print(query_syw('d',"010203#010204#B1","0102030405"))
--print('1,2,3,4,5,6,7,8,9,0,1,2,3')
--print(query_syw('d',"01051011#B1","0107091105"))
